package burubaka.controller.mypage;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import net.arnx.jsonic.JSON;

import org.slim3.controller.Navigation;

import burubaka.common.controller.Controller;
import burubaka.model.Tipster;
import burubaka.model.TotalResult;
import burubaka.service.TipsterService;
import burubaka.service.TotalResultService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class TotalResultController extends Controller {

	private static final Logger log = Logger.getLogger(TotalResultController.class.getName());

	private TipsterService tipsterService = new TipsterService();
	private TotalResultService totalResultService = new TotalResultService();

	@Override
	public Navigation init() throws Exception {

		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String userId = user.getUserId();
		Tipster tipster = tipsterService.findByUser(userId);

		TotalResult totalResult = totalResultService.getThisYearResult(tipster.getKey());
		Map<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("totalResult", totalResult);
		String json = JSON.encode(resMap, true);
		log.fine(json);
		setJson(json);

		return null;
	}
}
